//
//  ILGThirdDelegate.m
//  GameDemo
//
//  Created by Happy on 1/16/17.
//  Copyright © 2017 Happy. All rights reserved.
//

#import "ILGThirdDelegate.h"
#import <objc/runtime.h>

@implementation ILGThirdDelegate
+ (ILGThirdDelegate *)sharedInstance {
    static ILGThirdDelegate *share = nil;
    static dispatch_once_t predicate;
    dispatch_once(&predicate, ^{
        share = [[ILGThirdDelegate alloc] init];
    });
    return share;
}

- (void)setWxAppId:(NSString *)wxAppId {
    _wxAppId = wxAppId;

#ifdef ILGWXSDKIncluded
    [WXApi registerApp:wxAppId];
    NSLog(@"WXSDK Version:%@", [WXApi getApiVersion]);
#endif
}

- (void)setWbAppId:(NSString *)wbAppId {
    _wbAppId = wbAppId;
    
#ifdef ILGWBSDKIncluded
    [WeiboSDK registerApp:wbAppId];
#endif
}

#ifdef ILGQQSDKIncluded
static void *tencentOAuthKey = @"tencentOAuthKey";
- (void)setQqAppId:(NSString *)qqAppId {
    _qqAppId = qqAppId;
    self.tencentOAuth = [[TencentOAuth alloc]initWithAppId:qqAppId andDelegate:self];
}
- (void)setTencentOAuth:(TencentOAuth *)tencentOAuth {
    objc_setAssociatedObject(self, &tencentOAuthKey, tencentOAuth, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (TencentOAuth *)tencentOAuth {
    return objc_getAssociatedObject(self, &tencentOAuthKey);
}
#endif

- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
    BOOL resultValue = NO;
    
    BOOL wxValue = NO;
#ifdef ILGWXSDKIncluded
    wxValue = [WXApi handleOpenURL:url delegate:self];
#endif
    
    BOOL wbValue = NO;
#ifdef ILGWBSDKIncluded
    [WeiboSDK handleOpenURL:url delegate:self];
#endif
    
    BOOL qqValue = NO;
    BOOL qqShareValue = NO;
#ifdef ILGQQSDKIncluded
    qqValue = [TencentOAuth HandleOpenURL:url];
    qqShareValue = [QQApiInterface handleOpenURL:url delegate:self];
#endif
    
    return resultValue | wxValue | wbValue | qqValue | qqShareValue;
}

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
    return [self application:application handleOpenURL:url];
}

- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<NSString*, id> *)options{
    return [self application:app handleOpenURL:url];
}

+ (BOOL)isWXAppInstalled {
#ifdef ILGWXSDKIncluded
    return [WXApi isWXAppInstalled];
#endif
    return NO;
}

+ (void)openWeiXin {
#ifdef ILGWXSDKIncluded
    if ([WXApi isWXAppInstalled]) {
        [WXApi openWXApp];
    }else{
        [[UIApplication sharedApplication].keyWindow ilg_makeToast:@"请安装微信"];
    }
#endif
}

+ (BOOL)isQQAppInstalled {
#ifdef ILGQQSDKIncluded
    return [QQApiInterface isQQInstalled];
#endif
    return NO;
}

+ (void)openQQ {
#ifdef ILGQQSDKIncluded
    if([QQApiInterface isQQInstalled]) {
        [QQApiInterface openQQ];
    }else{
        [[UIApplication sharedApplication].keyWindow ilg_makeToast:@"请安装QQ"];
    }
#endif
}

@end


#ifdef ILGWXSDKIncluded
@implementation ILGThirdDelegate (WeiXin)
- (void)onReq:(BaseReq *)req {
    
}
-(void)onResp:(BaseResp*)resp {
    if([resp isKindOfClass:[SendAuthResp class]]) {
        // 微信登陆回调
        NSString *code = ((SendAuthResp *)resp).code;
        if (code) {
            self.loginCallback(nil, @{@"code":((SendAuthResp *)resp).code, @"appid":self.wxAppId}, ILGThirdTypeWeiXin);
            
        } else {
            NSError *error = [NSError errorWithDomain:@"com.zbsdk.login" code:resp.errCode userInfo:@{NSLocalizedDescriptionKey:resp.errStr ? resp.errStr:@"微信登录失败"}];
            self.loginCallback(error, @{}, ILGThirdTypeWeiXin);
            
        }
    } else if([resp isKindOfClass:[SendMessageToWXResp class]]) {
        if (resp.errCode == 0) {
            self.shareCallback(nil, @{}, ILGThirdTypeWeiXin);

        } else {
            NSError *error = [NSError errorWithDomain:@"com.zbsdk.share" code:resp.errCode userInfo:@{NSLocalizedDescriptionKey:resp.errStr ? resp.errStr:@"微信分享失败"}];
            self.shareCallback(error, @{}, ILGThirdTypeWeiXin);

        }
    }
}
@end
#endif


#if ILGShareFaceBookIsOpen
@implementation ILGThirdDelegate (FacebookShare)
- (void)sharer:(id<FBSDKSharing>)sharer didCompleteWithResults:(NSDictionary *)results {
    self.shareCallback(nil, @{}, ILGBShareTypeFacebook);
}

- (void)sharer:(id<FBSDKSharing>)sharer didFailWithError:(NSError *)error {
    self.shareCallback(error, @{}, ILGBShareTypeFacebook);
}

- (void)sharerDidCancel:(id<FBSDKSharing>)sharer {
    NSError *error = [NSError errorWithDomain:@"com.zcbsdk.share" code:-2 userInfo:@{NSLocalizedDescriptionKey:@"用户取消"}];
    self.shareCallback(error, @{}, ILGBShareTypeFacebook);
}
@end
#endif

#ifdef ILGWBSDKIncluded
@implementation ILGThirdDelegate (WeiBo)

- (void)didReceiveWeiboRequest:(WBBaseRequest *)request {}

- (void)didReceiveWeiboResponse:(WBBaseResponse *)response {
//    NSDictionary *responseUserInfo = response.userInfo;
    if ([response isKindOfClass:WBSendMessageToWeiboResponse.class]) //微博分享
    {
        if (response.statusCode == 0) {
            self.shareCallback(nil, @{}, ILGThirdTypeWeiBo);
        }
        else {
            NSError *error = [NSError errorWithDomain:@"com.zbsdk.share" code:response.statusCode userInfo:@{NSLocalizedDescriptionKey:@"分享失败"}];
            self.shareCallback(error, @{}, ILGThirdTypeWeiBo);
        }
    }
    else if ([response isKindOfClass:WBAuthorizeResponse.class]) //微博登录
    {
        if (response.statusCode == 0) {
            
        }
        else {
            
        }
    }
}

@end
#endif


#ifdef ILGQQSDKIncluded
@implementation ILGThirdDelegate (QQ)

#pragma mark TencentLoginDelegate

- (void)tencentDidLogin {}

/**
 * 登录失败后的回调
 * \param cancelled 代表用户是否主动退出登录
 */
- (void)tencentDidNotLogin:(BOOL)cancelled {}

/**
 * 登录时网络有问题的回调
 */
- (void)tencentDidNotNetWork {}


#pragma mark QQApiInterfaceDelegate
- (void)onReq:(QQBaseReq *)req {}

// 处理来至QQ的响应
- (void)onResp:(QQBaseResp *)resp {
    if ([resp isKindOfClass:[BaseResp class]]) {
        BaseResp *respWX = (BaseResp *)resp;
        if([resp isKindOfClass:[SendAuthResp class]]) {
            // 微信登陆回调
            NSString *code = ((SendAuthResp *)resp).code;
            if (code) {
                self.loginCallback(nil, @{@"code":((SendAuthResp *)resp).code, @"appid":self.wxAppId}, ILGThirdTypeWeiXin);
                
            } else {
                NSError *error = [NSError errorWithDomain:@"com.zbsdk.login" code:respWX.errCode userInfo:@{NSLocalizedDescriptionKey:respWX.errStr ? respWX.errStr:@"微信登录失败"}];
                self.loginCallback(error, @{}, ILGThirdTypeWeiXin);
                
            }
        } else if([resp isKindOfClass:[SendMessageToWXResp class]]) {
            if (respWX.errCode == 0) {
                self.shareCallback(nil, @{}, ILGThirdTypeWeiXin);
                
            } else {
                NSError *error = [NSError errorWithDomain:@"com.zbsdk.share" code:respWX.errCode userInfo:@{NSLocalizedDescriptionKey:respWX.errStr ? respWX.errStr:@"微信分享失败"}];
                self.shareCallback(error, @{}, ILGThirdTypeWeiXin);
                
            }
        }
    } else if ([resp isKindOfClass:[QQBaseResp class]]) {
        if (resp.type == ESENDMESSAGETOQQRESPTYPE) {
            SendMessageToQQResp* sendResp = (SendMessageToQQResp*)resp;
            if ([sendResp.result isEqualToString:@"0"]){
                self.shareCallback(nil, @{}, ILGThirdTypeQQ);
                
            } else {
                NSError *error = [NSError errorWithDomain:@"com.zbsdk.share" code:sendResp.result.intValue userInfo:@{NSLocalizedDescriptionKey:sendResp.errorDescription?:@"分享失败"}];
                self.shareCallback(error, @{}, ILGThirdTypeQQ);
            }
        }
    }
    
}
/**
 处理QQ在线状态的回调
 */
- (void)isOnlineResponse:(NSDictionary *)response {}

@end
#endif
